package com.huawei.esdk.tp.device.smc.v100r003c00.convert;

import java.util.List;

import javax.xml.datatype.XMLGregorianCalendar;

import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.tp.domain.model.Site;
import com.huawei.esdk.tp.domain.model.bean.PagedList;
import com.huawei.esdk.tp.domain.model.bean.QueryCondition;
import com.huawei.esdk.tp.domain.model.bean.QuerySites;
import com.huawei.esdk.tp.domain.model.bean.SortField;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.ArrayOfFilterBase;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.ArrayOfSortItem;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.DateTimeFilter;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.DoubleFilter;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.FilterBase;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.FilterColumn2;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.IPAddressEx;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.IPAddressFilter;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.IntFilter;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.PageParam;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.PagesInfo;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.QueryConfig;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.QuerySitesResponse;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.SiteParamEx;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.SortItem;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.SortType;
import com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.StringFilter;


public class TPSMC20V1R3C00SiteMgrConvert
{
    public com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.QuerySites querySites2QuerySitesSouth(QuerySites query)
    {
        com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.QuerySites south 
        = new com.huawei.esdk.tp.southcommu.cxf.gen.v100r003c00.QuerySites();
        QueryConfig config = new QueryConfig();
        // 排序参数
        ArrayOfSortItem sortItem = new ArrayOfSortItem();
         if (null != query.getSortField())
         {
             //做排序用
             for(SortField item : query.getSortField())
             {
                 SortItem sortItemSoap = new SortItem();
                 sortItemSoap.setSort(0 == item.getIsAscend() ? SortType.ASC : SortType.DESC);
                 sortItemSoap.setItemIndex(FilterColumn2Int2Enum(item.getFieldIndex()));
                 sortItem.getSortItem().add(sortItemSoap);
             }
             
         }
        config.setSortItems(sortItem);
        // 过滤参数
        ArrayOfFilterBase filter = new ArrayOfFilterBase();
         if (null != query.getQueryCondition())
         {
             //做过滤用
             for(QueryCondition item : query.getQueryCondition())
             {            
                 FilterBase fielter = getFilterModal2Soap(item);
                 if(null != fielter)
                 {
                     filter.getFilterBase().add(fielter);
                 }
             }
             
         }
        config.setFilters(filter);
        // 焦点数据
        config.setFocusItem(query.getFocusItem());
        // 分页数据'
        PageParam page = new PageParam();
        page.setCurrentPage(query.getCurrentPage());
        // 设定每页数为会场总数，一次性查出
        page.setNumberPerPage(query.getNumberPerPage());
        config.setPageParam(page);

        south.setQueryConfig(config);
        return south;
    }

    public SDKResult<PagedList<Site>> querySitesResponseSouth2SDKResult(
            QuerySitesResponse southResponse)
    {
        SDKResult<PagedList<Site>> result = new SDKResult<PagedList<Site>>();
        PagedList<Site> pagedList = new PagedList<Site>();
        if (null == southResponse)
        {
            result.setResult(pagedList);
            return result;
        }

        // 书写返回值
        result.setErrCode(southResponse.getQuerySitesResult().getErrCode());
        result.setSubSystem(String.valueOf(southResponse.getQuerySitesResult()
                .getSubSystem()));
        result.setDescription(southResponse.getQuerySitesResult()
                .getDescription());
        if (null != southResponse.getSites()
                && southResponse.getSites().getSiteParamEx().size() > 0)
        {
            List<SiteParamEx> siteParamEx = southResponse.getSites()
                    .getSiteParamEx();
            Site site = null;
            for (int i = 0; i < siteParamEx.size(); i++)
            {
                site = new Site();
                site.setUri(siteParamEx.get(i).getUri());
                site.setName(siteParamEx.get(i).getName());
                site.setSiteType(siteParamEx.get(i).getType().toString());
                site.setRate(siteParamEx.get(i).getRate());
                pagedList.addRecord(site);
            }
        }
        
        PagesInfo pageInfo = southResponse.getPageInfo();
        if(null != pageInfo)
        {
            pagedList.setCurPageNo(pageInfo.getCurrentPage());
            pagedList.setTotalPageCount(pageInfo.getTotalPages());
            pagedList.setTotalRecordCount(pageInfo.getTotalRecords());
        }

        result.setResult(pagedList);
        return result;
    }
    
    private FilterColumn2 FilterColumn2Int2Enum(Integer val)
    {
        switch(val){
            case 0 : return FilterColumn2.SITE_NAME;
            case 1 : return FilterColumn2.SITE_URI;
            case 2 : return FilterColumn2.SITE_STATUS;
            case 3 : return FilterColumn2.SITE_TYPE;
            case 4 : return FilterColumn2.CONF_TEMPLATE_NAME;
            case 5 : return FilterColumn2.CONF_TEMPLATE_ACCESS_CODE;
            case 6 : return FilterColumn2.MCU_NAME;
            default : return null;
        }
        
    }
    
    private FilterBase getFilterModal2Soap(QueryCondition item)
    {

        if(null == item)
        {
            return null;
        }
        if("1".equals(item.getConditionType()))
        {
            if(2 == item.getConditionValue().length)
            {
                StringFilter stringFilter = new StringFilter();
                stringFilter.setColumnIndex(FilterColumn2Int2Enum((Integer)item.getConditionValue()[0]));
                stringFilter.setValue((String)item.getConditionValue()[1]);
                return stringFilter;
            }
            else
            {
                return null;
            }
        }
        else if("2".equals(item.getConditionType()))
        {
            if(4 == item.getConditionValue().length)
            {
                IntFilter filter = new IntFilter();
                filter.setColumnIndex(FilterColumn2Int2Enum((Integer)item.getConditionValue()[0]));
                filter.setGreaterThanValue((Integer)item.getConditionValue()[1]);
                filter.setLessThanValue((Integer)item.getConditionValue()[2]);
                filter.setEqualToValue((Integer)item.getConditionValue()[3]);
                return filter;
            }
            else
            {
                return null;
            }
        }
        else if("3".equals(item.getConditionType()))
        {
            if(4 == item.getConditionValue().length)
            {
                DoubleFilter filter = new DoubleFilter();
                filter.setColumnIndex(FilterColumn2Int2Enum((Integer)item.getConditionValue()[0]));
                filter.setGreaterThanValue((Double)item.getConditionValue()[1]);
                filter.setLessThanValue((Double)item.getConditionValue()[2]);
                filter.setEqualToValue((Double)item.getConditionValue()[3]);
                return filter;
            }
            else
            {
                return null;
            }
        }
        else if("4".equals(item.getConditionType()))
        {
            if(3 == item.getConditionValue().length)
            {
                DateTimeFilter filter = new DateTimeFilter();
                filter.setColumnIndex(FilterColumn2Int2Enum((Integer)item.getConditionValue()[0]));
                filter.setGreaterThanValue((XMLGregorianCalendar)item.getConditionValue()[1]);
                filter.setLessThanValue((XMLGregorianCalendar)item.getConditionValue()[2]);
                return filter;
            }
            else
            {
                return null;
            }
        }
        else if("5".equals(item.getConditionType()))
        {
            if(2 == item.getConditionValue().length)
            {
                IPAddressFilter filter = new IPAddressFilter();
                filter.setColumnIndex(FilterColumn2Int2Enum((Integer)item.getConditionValue()[0]));
                IPAddressEx value = new IPAddressEx();
                value.setIPAddress((String)item.getConditionValue()[0]);
                filter.setValue(value);
                return filter;
            }
            else
            {
                return null;
            }
        }
        else
        {
            return null;
        }

    }
}
